<?php

namespace App\Http\Controllers;

use App\Models\RoleUser;
use App\Models\Throughtrain;
use App\Models\Role;
use Illuminate\Http\Request;
use \DB;
use App\Http\Requests;
use App\Http\Controllers\CommonController;
use Illuminate\Http\Response;
use App\Http\Requests\ZtcRequest;
use Illuminate\View\View;
use App\Models\User;

class ZtcController extends CommonController
{
    /**
     * 获取直通车列表
     *
     * @return Response
     */
    public function getList()
    {
        $lists = Throughtrain::paginate(10);

        return view('ztc.list')->with('lists', $lists)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     *
     */
    public function search(Request $request)
    {
        $data = $request->input();

        $sql = DB::table('throughtrain')
            ->leftjoin('user', 'throughtrain.uid', '=', 'user.uid')
            ->select('throughtrain.*', 'user.nickname');

        if (isset($data['nickname']) && $data['nickname'] != '') {
            $sql->where('user.nickname', 'like', '%' . $data['nickname'] . '%');
        }

        if (isset($data['status']) && $data['status'] != '') {
            $sql->where('throughtrain.status', '=', $data['status']);
        }

        if (isset($data['type']) && $data['type'] != '') {
            $sql->where('throughtrain.type', '=', $data['type']);
        }

        if (isset($data['time_type']) && $data['time_type'] != '') {
            $sql->where('throughtrain.time_type', '=', $data['time_type']);
        }

        $lists = $sql->paginate(10);

        return view('ztc.search')->with('lists', $lists)->with('nickname', $data['nickname'])
            ->with('status', $data['status'])->with('type', $data['type'])->with('time_type', $data['time_type'])->with("actions", $this->actions);
    }

    /**
     * 获取单个直通车详情以便编辑
     *
     * @return Response
     */
    public function getEdit(Request $request)
    {
        $id = $request->input('id');
        $list = Throughtrain::where('id', '=', $id)->first();

        return view('ztc.edit')->with('list', $list);
    }

    /**
     * 编辑某个直通车详情
     *
     * @param Request $request
     */
    public function postEdit(ZtcRequest $request)
    {
        $data = $request->input();
        unset($data['nickname'], $data['_token'], $data['formType']);
        $result = Throughtrain::where('id', $data['id'])->update($data);
        if ($result) {
            return AjaxCallbackMessage('直通车编辑成功', true, url('ztc/list'));
        } else {
            return AjaxCallbackMessage('直通车编辑失败，你没有修改任何信息或其他错误', false);
        }
    }

    /**
     *显示创建直通车的页面
     *
     * @return View
     */
    public function getCreate()
    {
        return view('ztc.create');
    }

    /**
     * 创建一个直通车
     *
     * @param Request $request
     *
     */
    public function postInsert(ZtcRequest $request)
    {
        $data = $request->input();
        $username = $data['username'];
        $user = User::where('username', $username)->first();
        if (!is_object($user)) {
            return AjaxCallbackMessage('直通车新增失败,没有该投资人', false);
        }

//        if ($user->is_ztc!=1) {
//            return AjaxCallbackMessage('直通车新增失败,该投资人没有创建直通车的资格', false);
//        }
        $exist = Throughtrain::where('uid', $user->uid)->first();

        if (is_object($exist)) {
            return AjaxCallbackMessage('直通车新增失败,该投资人已经创建过直通车', false);
        }
        unset($data['username'], $data['_token'], $data['formType']);
        $data['uid'] = $user->uid;
        $result = Throughtrain::create($data);
        User::where('uid', $data['uid'])->update(['is_ztc' => 1,]);
        $role = Role::where('name', '投资人')->first();
        RoleUser::where('uid', $data['uid'])->update(['role_id'=>$role->id]);
        if (is_object($result)) {
            return AjaxCallbackMessage('直通车新增成功,该用户身份已经转变为投资人，并且具备开通直通车的资格。', true, url('ztc/list'));
        } else {
            return AjaxCallbackMessage('直通车新增失败', false);
        }
    }
}
